package cn.iocoder.yudao.module.bpm.controller.admin.projectinfo;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.bpm.controller.admin.projectinfo.vo.*;
import cn.iocoder.yudao.module.bpm.dal.dataobject.projectinfo.BpmProjectInfoDO;
import cn.iocoder.yudao.module.bpm.service.projectinfo.BpmProjectInfoService;

@Tag(name = "管理后台 - 流程项目信息")
@RestController
@RequestMapping("/bpm/project-info")
@Validated
public class BpmProjectInfoController {

    @Resource
    private BpmProjectInfoService projectInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建流程项目信息")
    @PreAuthorize("@ss.hasPermission('bpm:project-info:create')")
    public CommonResult<Long> createProjectInfo(@Valid @RequestBody BpmProjectInfoSaveReqVO createReqVO) {
        return success(projectInfoService.createProjectInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新流程项目信息")
    @PreAuthorize("@ss.hasPermission('bpm:project-info:update')")
    public CommonResult<Boolean> updateProjectInfo(@Valid @RequestBody BpmProjectInfoSaveReqVO updateReqVO) {
        projectInfoService.updateProjectInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除流程项目信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('bpm:project-info:delete')")
    public CommonResult<Boolean> deleteProjectInfo(@RequestParam("id") Long id) {
        projectInfoService.deleteProjectInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得流程项目信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bpm:project-info:query')")
    public CommonResult<BpmProjectInfoRespVO> getProjectInfo(@RequestParam("id") Long id) {
        BpmProjectInfoDO projectInfo = projectInfoService.getProjectInfo(id);
        return success(BeanUtils.toBean(projectInfo, BpmProjectInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得流程项目信息分页")
    @PreAuthorize("@ss.hasPermission('bpm:project-info:query')")
    public CommonResult<PageResult<BpmProjectInfoRespVO>> getProjectInfoPage(@Valid BpmProjectInfoPageReqVO pageReqVO) {
        PageResult<BpmProjectInfoDO> pageResult = projectInfoService.getProjectInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BpmProjectInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出流程项目信息 Excel")
    @PreAuthorize("@ss.hasPermission('bpm:project-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProjectInfoExcel(@Valid BpmProjectInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BpmProjectInfoDO> list = projectInfoService.getProjectInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "流程项目信息.xls", "数据", BpmProjectInfoRespVO.class,
                        BeanUtils.toBean(list, BpmProjectInfoRespVO.class));
    }

}